﻿/*
 *
 * Autoload.cs
 * For autoload.php in CI
 * 
 * Author @semarketir
 * 
 * CICI is simple tool for generate Controller, Model, and View in CodeIgniter ( or CI ) Framework
 * CICI can used to setup configurations in CI ( Database, Autoload, Config ) <- Next Version
 * CICI just can to used for default directory of CodeIgniter ( or CI ) Framework
 * 
 * CICI v.0.0.4 Beta
 * 
 * Support with CI 2.X ( or heigh )
 * 
 * License Under GPL
 * 
 * 
 * Salam, 
 * semar ketir
 * 
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Text.RegularExpressions;

namespace cici
{
    class Autoload
    {
        private string path_application;
        private string autoload_file;
        private StreamWriter streamWritter;
        private StreamReader streamReader;

        public Autoload()
        {
            this.path_application = Environment.CurrentDirectory + @"\application\config";
            this.autoload_file = this.path_application + @"\autoload.php";
        }
        public Boolean createAutoload(string autoloadConfig)
        {
            Boolean status = false;

            string[] temps = autoloadConfig.Split(';');

            Dictionary<string, string> temp_dic = new Dictionary<string, string>();

            foreach (string temp in temps)
            {
                string[] temp_abis = temp.Split('=');
                if (temp_abis.Length > 1)
                {
                    if (temp_dic.ContainsKey(temp_abis[0]))
                    {
                        temp_dic[temp_abis[0]]=temp_dic[temp_abis[0]]+"','"+temp_abis[1];
                    }
                    else
                    {
                        temp_dic.Add(temp_abis[0], temp_abis[1]);
                    }
                }
            }
            if (Directory.Exists(this.path_application))
            {
                if (File.Exists(this.autoload_file))
                {
                    this.streamReader = new StreamReader(this.autoload_file);

                    string temp_string = this.streamReader.ReadToEnd();

                    string output_regex = temp_string;

                    foreach (KeyValuePair<string, string> pair in temp_dic)
                    {
                        output_regex = Regex.Replace(output_regex,
                            "\\$autoload\\[?(\"|')" + pair.Key + "?(\"|')\\](\\s*)=(\\s*)array\\(",
                            "$autoload['" + pair.Key + "'] = array('" + pair.Value + "',");

                        output_regex = Regex.Replace(output_regex,"\\,\\)\\;",");");
                    }

                    output_regex += "\n/* Generated by CICI */";

                    this.streamReader.Close();

                    this.streamWritter = new StreamWriter(this.autoload_file);
                    this.streamWritter.Write(output_regex);
                    this.streamWritter.Flush();
                    this.streamWritter.Close();

                    status = true;
                }
            }


            return status;
        }
    }
}
